from typing import List, Dict, Any, Literal
from db.operations import BaseOperations


class MainCategoryOperations(BaseOperations):
    """图书主分类数据库操作类"""

    def __init__(self):
        # 继承BaseOperations，指定表名为book_main_categories
        super().__init__("book_main_categories")

    async def batch_insert_main_categories(
        self,
        categories_data: List[Dict[str, Any]],
        on_duplicate: Literal["skip", "update"] = "skip",
    ) -> int:
        """
        批量插入图书主分类数据

        Args:
            categories_data (List[Dict[str, Any]]): 主分类数据列表
            每个字典应包含主分类的字段，例如：
            {
                "book_id": 1,
                "main_category": "文学"
            }
            on_duplicate (str): 重复处理策略 - "skip"(跳过), "update"(更新)

        Returns:
            int: 成功插入的主分类记录数量
        """
        if not categories_data:
            return 0

        # 调用父类的批量插入方法
        return await self.batch_insert(categories_data, on_duplicate=on_duplicate)

    async def batch_query_main_categories_by_book_ids(
        self, book_ids: List[int]
    ) -> List[Dict[str, Any]]:
        """
        根据批量 book_id 查询主分类信息

        Args:
            book_ids (List[int]): 图书ID列表

        Returns:
            List[Dict[str, Any]]: 查询到的主分类信息列表
        """
        if not book_ids:
            return []

        # 构建 IN 子句的占位符
        placeholders = ", ".join(["%s"] * len(book_ids))
        where_clause = f"book_id IN ({placeholders})"

        # 调用父类的批量查询方法
        return await self.batch_query(where_clause=where_clause, params=tuple(book_ids))
